<?php

namespace app\services;

class SdkService extends ServiceBase
{
    /**
     * 获得签名
     * @param  string $appkey 签名key
     * @param  array  $params 待签名参数
     * @return string
     */
    private function getSign($appkey, $params)
    {
        $arr = [];
        ksort($params);
        foreach ($params as $key => $val) {
            if ($key === 'sign') continue;
            if ($val === '' || $val === 'null' || is_null($val)) continue;
            $arr[] = "$key=$val";
        }
        $str  = implode("&", $arr) . "$appkey";
        $sign = md5(strtolower($str));

        return $sign;
    }

    public function checkSign($appkey, $params)
    {
        if (!array_key_exists('sign', $params)) {
            return false;
        }
        $sign       = $params['sign'];
        $check_sign = $this->getSign($appkey, $params);
        if (strtolower($sign) !== strtolower($check_sign)) {
            return false;
        }

        return true;
    }
}
